home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Sample Code / System 7.0 Samples / Edition Manager / AppleEventM.c next >
Encoding:
C/C++ Source or Header  |  1991-02-13  |  12.0 KB  |  290 lines  |  [TEXT/MPS ]

  1. /*------------------------------------------------------------------------------
  2.  *
  3.  *  Apple Developer Technical Support
  4.  *
  5.  *  AppleEvent specific routines
  6.  *
  7.  *  Program:    EditionSample
  8.  *  File:       AppleEventM.c - C Source
  9.  *
  10.  *  by:         C.K. Haun <TR>
  11.  *
  12.  *  Copyright © 1990 Apple Computer, Inc.
  13.  *  All rights reserved.
  14.  *
  15.  *------------------------------------------------------------------------------
  16.  * This file handles the generic AppleEvent handlers, as well as intialization
  17.  * and installing the handlers I'll need for this application, specifically the 
  18.  * Edition Manager event handlers
  19.  *----------------------------------------------------------------------------*/
  20.  
  21.  
  22. #define __AEM__
  23. /* temporary, until it gets into the equates */
  24. #define kAECreatePublisher 'cpub'
  25.  
  26. #pragma segment Main
  27. #pragma load "EdSampheaders"                                /* see the Buildheaders.c file */
  28.  
  29. #include "EdSampdefines.h"
  30.  
  31. /* prototypes */
  32. void DoHighLevel(EventRecord AERecord);
  33. void InitAEStuff(void);
  34. pascal OSErr AEOpenHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
  35. pascal OSErr AEOpenDocHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
  36. pascal OSErr AEPrintHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
  37. pascal OSErr AEQuitHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
  38. Boolean MissedAnyParameters(AppleEvent *message);
  39. OSErr processOpenPrint(AppleEvent *messagein, Boolean printIt);
  40. OSErr GetSectionHandleFromEvent(AppleEvent *AEin, SectionHandle *theSection);
  41. extern void ShowMe(Str255 in, OSErr aevtErr);
  42.  
  43. /* external functions */
  44. extern OSErr PrepQuit();
  45. extern pascal OSErr AEReadSectionHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
  46. extern pascal OSErr AEWriteSectionHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
  47. extern pascal OSErr AEScrollSectionHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
  48. extern pascal OSErr AECancelSectionHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
  49. extern pascal OSErr AECreatePubHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
  50. extern WindowPtr OpenFile(FSSpec *inSpec);
  51. extern void PrintIt(WindowPtr theWind);
  52. extern WindowPtr AddNewWindow(Boolean showIt);
  53. extern void ChangePlane(WindowPtr twindow);
  54. extern void CloseMyWindow(WindowPtr theClose);
  55.  
  56. /* DoHighLevel processes our high level events */
  57. void DoHighLevel(EventRecord AERecord)
  58. {
  59.     OSErr fred2;
  60.     fred2 = AEProcessAppleEvent(&AERecord);
  61.     if ((fred2 != userCanceledErr) && (fred2 != noErr) && (fred2 != errAEEventNotHandled))
  62.         ShowMe("\pAppleEvent Proccessing.", fred2);
  63.     /* if it was a userCanceledErr (from the quit routine) and the reply has been */
  64.     /* sent from there already */
  65.     /* if it's a errAEEventNotHandled, then someone sent us  an event we're not prepared for, */
  66.     /* that is not a reason to put up a dialog.  Since anyone can send us events, then */
  67.     /* we could get all kinds of stray stuff, so just ignore it. */
  68.     
  69. }
  70.  
  71. /* end DoHighLevel */
  72.  
  73. /* InitAEStuff checks to see if this machine has AppleEvents and 
  74. *   does our setup.
  75. *   If AppleEvents are not found, we alert and exit.
  76. *   This is also the place where all the handlers for AppleEvents we deal
  77. *   with are installed.  This includes the required AppleEvents, and the
  78. *   Edition Manager specific ones used in this app.
  79. */
  80. #pragma segment MyInit
  81. void InitAEStuff(void)
  82. {
  83.     extern Boolean gHasAppleEvents;
  84.     extern long aLong;
  85.     OSErr aevtErr = noErr;
  86.     aLong = 0;
  87.     /* Check this machine for AppleEvents.  If they are not here (ie not 7.0)
  88.     *   then we exit */
  89.     gHasAppleEvents = (Gestalt(gestaltAppleEventsAttr, &aLong) == noErr);
  90.     /* The following series of calls installs all our AppleEvent Handlers.
  91.     *   These handlers are added to the application event handler list that 
  92.     *   the AppleEvent manager maintains.  So, whenever an AppleEvent happens
  93.     *   and we call AEProcessEvent, the AppleEvent manager will check our
  94.     *   list of handlers and dispatch to it if there is one.
  95.     */
  96.     if (gHasAppleEvents) {
  97.         if (aevtErr == noErr)
  98.             /* First the four required events, OAPP,ODOC,PDOC, and QUIT */
  99.             aevtErr = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, (ProcPtr)AEOpenHandler, 0, false);
  100.         else
  101.             ShowMe("\pInstalling AppleEvent handler.", aevtErr);
  102.         if (aevtErr == noErr)
  103.             aevtErr = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, (ProcPtr)AEOpenDocHandler, 0, false);
  104.         else
  105.             ShowMe("\pInstalling AppleEvent handler.", aevtErr);
  106.         if (aevtErr == noErr)
  107.             aevtErr = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, (ProcPtr)AEPrintHandler, 0, false);
  108.         else
  109.             ShowMe("\pInstalling AppleEvent handler.", aevtErr);
  110.         if (aevtErr == noErr)
  111.             aevtErr = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, (ProcPtr)AEQuitHandler, 0, false);
  112.         else
  113.             ShowMe("\pInstalling AppleEvent handler.", aevtErr);
  114.         /* Now the Edition Manager events */
  115.         if (aevtErr == noErr)
  116.             aevtErr = AEInstallEventHandler(kCoreEventClass, kAECreatePublisher, (ProcPtr)AECreatePubHandler, 0, false);
  117.  
  118.         if (aevtErr == noErr)
  119.             aevtErr = AEInstallEventHandler(sectionEventMsgClass, sectionReadMsgID, (ProcPtr)AEReadSectionHandler, 0, false);
  120.         else
  121.             ShowMe("\pInstalling AppleEvent handler.", aevtErr);
  122.         if (aevtErr == noErr)
  123.             aevtErr = AEInstallEventHandler(sectionEventMsgClass, sectionWriteMsgID, (ProcPtr)AEWriteSectionHandler, 0, false);
  124.         else
  125.             ShowMe("\pinstall", aevtErr);
  126.         if (aevtErr == noErr)
  127.             aevtErr = AEInstallEventHandler(sectionEventMsgClass, sectionScrollMsgID, (ProcPtr)AEScrollSectionHandler, 0, false);
  128.         else
  129.             ShowMe("\pInstalling AppleEvent handler.", aevtErr);
  130.         if (aevtErr == noErr)
  131.             aevtErr = AEInstallEventHandler(sectionEventMsgClass, sectionCancelMsgID, (ProcPtr)AECancelSectionHandler, 0, false);
  132.         else
  133.             ShowMe("\pInstalling AppleEvent handler.", aevtErr);
  134.     } else {
  135.         Alert(kNoAppleEvents, nil);
  136.         ExitToShell();
  137.     }
  138. }
  139.  
  140. /* end InitAEStuff */
  141. #pragma segment Main
  142. /***************************************************************************************
  143.  
  144.     MissedAnyParameters
  145.  
  146.     Used to check for any unread required parameters. Returns true if we missed at
  147.     least one.
  148.  
  149. *****************************************************************************************/
  150. Boolean MissedAnyParameters(AppleEvent *message)
  151. {
  152.     OSErr err;
  153.     DescType ignoredActualType;
  154.     AEKeyword missedKeyword;
  155.     Size ignoredActualSize;
  156.     EventRecord event;
  157.     
  158.     err = AEGetAttributePtr(message, keyMissedKeywordAttr, typeKeyword, &ignoredActualType, (Ptr)&missedKeyword,
  159.                             sizeof(missedKeyword), &ignoredActualSize);
  160.     
  161.     /* no error means that we found some more.*/
  162.     
  163.     if (err == noErr) {
  164.         event.message = *(long *)&ignoredActualType;
  165.         event.where = *(Point *)&missedKeyword;
  166.         ShowMe("\pMissedAnyParameters: got parameters I don't know what to do with.", err);
  167.         err = errAEEventNotHandled;
  168.     }
  169.     
  170.     /* errAEDescNotFound means that there are no more parameters. If we get */
  171.     /* an error code other than that, flag it. */
  172.     
  173.     else if (err != errAEDescNotFound) {
  174.         ShowMe("\pMissedAnyParameters: after AEGetAttributeDesc.", err);
  175.     }
  176.     return(err != errAEDescNotFound);
  177. }
  178.  
  179. /* This is the standard Open Application event.  You'll get this as one of the (if not the ) */
  180. /* first events in your application.  So, we open up a blank document */
  181. pascal OSErr AEOpenHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
  182. {
  183. #pragma unused (messagein,reply,refIn)
  184.     ChangePlane(AddNewWindow(true));                        /* open our initial, blank window */
  185.     /* and make sure it's the front window */
  186.     return(noErr);
  187. }
  188.  
  189. /* end AEOpenHandler */
  190.  
  191. /* Open Doc, opens our documents.  Remember, this can happen at application start AND */
  192. /* anytime else.  If your app is up and running and the user goes to the desktop, hilites one */
  193. /* of your files, and double-clicks or selects Open from the finder File menu this event */
  194. /* handler will get called. Which means you don't do any initialization of globals here, or */
  195. /* anything else except open then doc.  */
  196. /* SO-- Do NOT assume that you are at app start time in this */
  197. /* routine, or bad things will surely happen to you. */
  198.  
  199. pascal OSErr AEOpenDocHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
  200. {
  201. #pragma unused (reply, refIn)
  202.     processOpenPrint(messagein, false);
  203. }
  204.  
  205. pascal OSErr AEPrintHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
  206. {                                                           /* no printing handler in yet, so we'll ignore this */
  207.     /* the operation is functionally identical to the ODOC event, with the additon */
  208.     /* of calling your print routine.  */
  209. #pragma unused (reply,refIn)
  210.     processOpenPrint(messagein, true);
  211.     return(noErr);
  212. }
  213.  
  214. /* Standard Quit event handler, to handle a Quit event from the Finder, for example.  */
  215. /* ••••• DO NOT CALL EXITTOSHELL HERE ••••• or you will never have a happy life.  */
  216. pascal OSErr AEQuitHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
  217. {
  218. #pragma unused (messagein,refIn)
  219.     OSErr theErr;
  220.     Str32 userCanx = "\pUser canceled";
  221.     theErr = PrepQuit();                                    /* prepQuit sets the Stop flag for us.  It does _NOT_ quit, you */
  222.     /* should NEVER quit from an AppleEvent handler.  Calling */
  223.     /* ExitToShell here would blow things up */
  224.     if (theErr == noErr)
  225.         return(noErr);
  226.     if (theErr == (userCanceledErr)) {
  227.         /* reply to the application that told us to quit */
  228.         AEPutParamPtr(reply, 'errn', typeLongInteger, (Ptr)&theErr, sizeof(OSErr));
  229.         AEPutParamPtr(reply, 'errs', typeChar, (Ptr)userCanx, userCanx[0]);
  230.     } else
  231.         return(theErr);
  232.     
  233. }
  234.  
  235. /* my routine to snatch a section handle out of an AppleEvent */
  236. OSErr GetSectionHandleFromEvent(AppleEvent *AEin, SectionHandle *theSection)
  237. {
  238.     DescType thisType;
  239.     Size returnedSize;
  240.     return(AEGetKeyPtr(AEin, keyDirectObject, typeSectionH, &thisType, (Ptr)theSection, sizeof(theSection), &returnedSize));
  241. }
  242.  
  243. /* processOpenPrint handles ODOC and PDOC events.  Both events open a document, one prints it */
  244. OSErr processOpenPrint(AppleEvent *messagein, Boolean printIt)
  245. {
  246.     OSErr err;
  247.     OSErr err2;
  248.     AEDesc theDesc;
  249.     FSSpec theFSS;
  250.     short loopy;
  251.     long numFilesToOpen;
  252.     AEKeyword ignoredKeyWord;
  253.     DescType ignoredType;
  254.     Size ignoredSize;
  255.     WindowPtr tWind;
  256.     err = AEGetParamDesc(messagein, keyDirectObject, typeAEList, &theDesc);
  257.     if (err) {
  258.         ShowMe("\pAEGetParamDesc.", err);
  259.     }
  260.     if (!MissedAnyParameters(messagein)) {
  261.         
  262.         /* Got all the parameters we need. Now, go through the direct object, */
  263.         /* see what type it is, and parse it up. */
  264.         
  265.         if (err = AECountItems(&theDesc, &numFilesToOpen))
  266.             ShowMe("\pAECountItems.", err);
  267.         else {
  268.             for (loopy = 1; ((loopy <= numFilesToOpen) && (!err)); ++loopy) {
  269.                 if (err = AEGetNthPtr(&theDesc, loopy, typeFSS, &ignoredKeyWord, &ignoredType, (Ptr)&theFSS, sizeof(theFSS),
  270.                                       &ignoredSize))
  271.                     ShowMe("\pAEGetNthDesc.", err);
  272.                 else
  273.                     tWind = OpenFile(&theFSS);
  274.                 if (printIt && tWind != nil) {
  275.                     
  276.                     PrintIt(tWind);                         /* in Print.c.  Does not yet print, but the idea is there */
  277.                     CloseMyWindow(tWind);
  278.                 }
  279.             }                                               /* for loopy = ... */
  280.         }                                                   /* AECountItems OK */
  281.     }                                                       /* Got all necessary parameters */
  282.     
  283.     if (err2 = AEDisposeDesc(&theDesc))
  284.         ShowMe("\pAEDisposeDesc of theDesc.", err2);
  285.     return(err ? err : err2);
  286. }
  287.  
  288.  
  289. #undef __AEM__
  290.